package com.hyj.jpdy.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.hyj.jpdy.bean.vo.User;
import com.hyj.jpdy.job.HotspotGrap;
import com.hyj.jpdy.service.ILoginService;


@Controller
@SessionAttributes("flag")
@RequestMapping("/sys/user/*")
public class LoginAction {

	Log log = LogFactory.getLog(LoginAction.class);
	@Autowired
	@Qualifier("loginService")
    private ILoginService loginService;
	
	//@Autowired
	//private HotspotGrap grap;
	
	/**
	 * 默认 是request set  map 的所有属性值
	 * @param u
	 * @param map
	 * @return
	 */
    @RequestMapping(value="login.do", method=RequestMethod.POST)
    public String login(User u,ModelMap map)
    {
    	log.info("hello login");
    	
    	loginService.saveUser(u);
    	map.put("user", u);
    	map.put("flag", "huyj");
    	return "view/sys/login";
    }
    
    /*@RequestMapping(value="login.do", method=RequestMethod.POST)
    public ModelAndView login(User u)
    {
    	log.info("hello login");
    	ModelAndView  view = new ModelAndView("view/sys/login");
    	loginService.saveUser(u);
    	view.addObject("user", u);
    	view.addObject("flag", "huyj");
    	return view;
    }*/
    
    /**
     * 
     */
    @RequestMapping(value="regist.do", method=RequestMethod.POST)
    public String login(User userparams, String code, ModelMap map)
    {
        System.out.println("--->");
        loginService.saveUser(new User());
        // 检查验证码
        if(code.equals("1234"))
        {
            User user = loginService.getUser(userparams.getAccount(), userparams.getPassword());
            if(user == null)
            {
                map.addAttribute("ERROR", "登陆失败,请检查用户名密码!");
                return "view/sys/login";
            }
            else
            {
                
                return "view/main";
            }
        }
        
        map.addAttribute("ERROR", "验证码错误");
        return "view/sys/login";
    }
    
    @RequestMapping(value="grapJoke.do", method=RequestMethod.POST)
    public String grapJoke(User u,ModelMap map)
    {
    	log.info("grap joke...");
    	
    	//loginService.saveUser(u);
    	//grap.grapJoke();
    	// grap.grapSina();
    	//grap.grapRss();
    	map.put("user", u);
    	map.put("flag", "huyj");
    	return "view/sys/login";
    }
}
